﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using MySql.Data.MySqlClient;
using BBMS.DLL.Base;

namespace BBMS.DLL.DataContract
{
    public class NhanVienQuyen : BBMS.DLL.Base.ObjectBase
    {
        public int IDNhanVien;
        public string Ma;
        public string Quyen;

        public NhanVienQuyen()
        {
        }

        public NhanVienQuyen(int IDNhanVien, string Ma, string Quyen)
        {
            this.IDNhanVien = IDNhanVien;
            this.Ma = Ma;
            this.Quyen = Quyen;
        }

        public override object Key
        {
            get
            {
                return this.IDNhanVien;
            }
        }
        public override string Name
        {
            get
            {
                return this.Ma;
            }
        }
        public override string TableName
        {
            get
            {
                return "UserRole";
            }
        }

        public override string[] KeyFields
        {
            get
            {
                return new string[] { "IDNhanVien" };
            }
        }
        public override string[] IDFields
        {
            get
            {
                return new string[] { "IDNhanVien" };
            }
        }
        public override System.Data.DataTable GetObjectDataTable()
        {
            System.Data.DataTable dt = new System.Data.DataTable(TableName);
            dt.Columns.Add("IDNhanVien", typeof(Int32));
            dt.Columns.Add("Ma", typeof(String));
            dt.Columns.Add("Quyen", typeof(String));
            return dt;
        }
        public override System.Data.DataRow ObjectToDataRow()
        {
            System.Data.DataRow row = this.GetObjectDataTable().NewRow();
            row["IDNhanVien"] = this.IDNhanVien;
            row["Ma"] = this.Ma;
            row["Quyen"] = this.Quyen;
            return row;
        }

        public override BBMS.DLL.Base.ObjectBase DataRowToObject(System.Data.DataRow row)
        {
            NhanVienQuyen obj = null;
            try
            {
                obj = new NhanVienQuyen(Convert.ToInt32(row["IDNhanVien"].ToString()), row["Ma"].ToString(), row["Quyen"].ToString());
            }
            catch { }
            return obj;
        }

        public override BBMS.DLL.Base.ObjectBase DataReaderToObject(MySql.Data.MySqlClient.MySqlDataReader rd)
        {
            NhanVienQuyen obj = null;
            try
            {
                obj = new NhanVienQuyen(Convert.ToInt32(rd["IDNhanVien"].ToString()), rd["Ma"].ToString(),
                     rd["Quyen"].ToString());
            }
            catch { }
            return obj;
        }

        public static string InsertRole(int IDNhanVien, Hashtable functionRoleList)
        {
            string msg = "";
            if (functionRoleList.Count > 0)
            {
                //foreach (DictionaryEntry obj in functionRoleList)
                //{ 
                //}
                MySqlTransaction tn = null;
                try
                {
                    MySqlConnection con = new MySqlConnection(Config.ConnectionString());
                    con.Open();
                    tn = con.BeginTransaction();

                    MySqlCommand cmd1 = con.CreateCommand();
                    cmd1.CommandType = System.Data.CommandType.Text;
                    cmd1.CommandText = "delete from UserRole where IDNhanVien = @IDNhanVien;";
                    cmd1.Parameters.Clear();
                    cmd1.Parameters.Add("@IDNhanVien", MySqlDbType.Int32);
                    cmd1.Parameters[0].Value = IDNhanVien;
                    cmd1.Transaction = tn;
                    cmd1.ExecuteNonQuery();
                   // for (int i = 0; i < obj.Count; i++)
                    foreach (DictionaryEntry obj in functionRoleList)                   
                    {
                        if (obj.Value.ToString() != "0000")
                        {
                            MySqlCommand cmd = con.CreateCommand();
                            cmd.CommandType = System.Data.CommandType.Text;
                            cmd.CommandText = "insert into UserRole (IDNhanVien,Ma,Quyen) values (@IDNhanVien,@Ma,@Quyen)";
                            cmd.Transaction = tn;
                            cmd.Parameters.Clear();
                            cmd.Parameters.Add("@IDNhanVien", MySqlDbType.Int32);
                            cmd.Parameters[0].Value = IDNhanVien;
                            cmd.Parameters.Add("@Ma", MySqlDbType.VarString);
                            cmd.Parameters[1].Value = obj.Key.ToString();
                            cmd.Parameters.Add("@Quyen", MySqlDbType.VarString);
                            cmd.Parameters[2].Value = obj.Value.ToString();
                            cmd.ExecuteNonQuery();
                        }

                    }


                    tn.Commit();
                    msg = "";
                    con.Close();
                    con.Dispose();
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex.StackTrace);
                    msg = "Lỗi xử lý cơ sở dữ liệu.";
                    tn.Rollback();
                }
                finally
                {

                    tn.Dispose();
                }
            }
            return msg;
        }

    }
}
